<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class layoutBlocks extends CWidget
{

	public $block = '';
	static $blocks = [ ];
	static $cached = false;

	public function run()
	{
		if( empty( self::$blocks ) && !self::$cached )
		{
			$cont = Yii::app()->controller->id;
			$action = Yii::app()->controller->action->id;
			$curl = Yii::app()->request->url;

			$query = "SELECT * FROM {{blocks}} WHERE \"blockActive\" =1 AND (\"blockAction\"='" . $cont . "." . $action . "' OR \"blockAction\"='{$cont}.*' OR \"blockUrl\"='{$curl}') ORDER BY \"blockOrder\" ASC;";
			self::$blocks = Yii::app()->db->createCommand( $query )->queryAll( TRUE );
			self::$cached = true;
		}
		if( empty( self::$blocks ) )
			return;
		foreach( self::$blocks as $block )
		{
			if( $block[ 'blockPosition' ] != $this->block )
				continue;
			if( !empty( $block[ 'blockContent' ] ) )
			{
				$this->render( 'blocks', array( 'content' => $block[ 'blockContent' ] ) );
			}
			elseif( !empty( $block[ 'blockWidget' ] ) )
			{
                            
				$widget = explode( ".", $block[ 'blockWidget' ] );
				$this->render( 'blocks', array( 'content' => 'application.modules.' . $widget[ 0 ] . '.widgets.' . $widget[ 1 ], 'widget'=>true ) );
//                                if ($block[ 'blockWidget' ] == 'main.cityContacts'){
//                                $this->render( 'blocks', array( 'content' => 'application.modules.main.widgets.Call', 'widget'=>true ) );
//                            }
			}
		}
	}

}
